package com.hengxinyongli.iam;

import com.hengxinyongli.iam.dto.Subject;
import com.hengxinyongli.iam.entity.Resource;
import com.hengxinyongli.iam.jwt.JwtAuthService;
import com.hengxinyongli.iam.mapper.ResourceMapper;
import com.hengxinyongli.iam.remote.IamManageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author magic
 * @date 2018/9/19/019 11:43
 * @version 1.0
 * Description Application
 */
@SpringBootApplication
@EnableSwagger2
@EnableCaching
public class Application implements CommandLineRunner {

    Logger logger = LoggerFactory.getLogger(Application.class);

    @Autowired
    JwtAuthService jwtAuthService;

    @Autowired
    ResourceMapper resourceMapper;

    @Autowired
    IamManageService iamManageService;

    public static void main(String[] args) {

        SpringApplication.run(Application.class,args);
    }

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.hengxinyongli.iam.web"))
                .paths(PathSelectors.any())
                .build();
    }

    public ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Iam Swagger2  RESTful APIs")
                .description("iam 身份认证授权服务")
                .termsOfServiceUrl("")
                .contact("")
                .version("1.0")
                .build();
    }

    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                        .allowedOrigins("*")
                        .allowedMethods("*")
                        .allowedHeaders("*")
                        .allowCredentials(true)
                        .maxAge(3600);
            }
        };
    }

    @Override
    public void run(String... args) throws Exception {
        //List<Subject> subjectByDepartType = iamManageService.getSubjectByDepartType("it", true);
        //System.out.println(subjectByDepartType);
        /*List<Resource> resources = resourceMapper.selectByModelSelective(null,true);
        List<Resource> res = resources.stream().filter(p ->
            Long.parseLong(p.getId()) >= 60000017L && Long.parseLong(p.getId()) <= 600000164L
        ).sorted((p1,p2) -> p1.getLevel().compareTo(p2.getLevel())).collect(Collectors.toList());
        System.out.println(res);

        createUrl(res,"mfq");
        createUrl(res,"langbei");
        createUrl(res,"qubei");*/
    }

    private void createUrl(List<Resource> res,String remark) {
        Map<String,String> map = new HashMap<>(256);
        map.put("-1","-1");
        res.forEach(p->{
            Resource resource = new Resource();
            resource.setCode(p.getCode());
            resource.setType(p.getType());
            resource.setOrderSeq(p.getOrderSeq());
            resource.setLevel(p.getLevel());
            resource.setCreateAt(new Date());
            resource.setUpdateAt(new Date());
            resource.setName(p.getName());
            resource.setRemark(remark);
            resource.setParentId(map.get(p.getParentId()));
            resource.setUrl(p.getUrl());
            String id = resourceMapper.generateId("6000");
            resource.setId(id);
            resourceMapper.insertSelective(resource);
            map.put(p.getId(),resource.getId());
        });
    }
}
